package com.lkx.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkx.common.JwtUtil;
import com.lkx.common.R;
import com.lkx.pojo.Employee;
import com.lkx.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName: com.lkx.controller.EmployeeController
 * @author: likaixin
 * @create: 2022年11月19日 17:24
 * @desc:  登录和退出登录处理
 */
@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        String pwd = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes(StandardCharsets.UTF_8));
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lambdaQueryWrapper);
        if (emp==null){
            return R.error("用户名不存在！");
        }
        if (!emp.getPassword().equals(pwd)){
            return R.error("密码输入错误！请重试！");
        }
        if (emp.getStatus()==0){
            return R.error("账号已被禁用！");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }
    /**
     * @Date:  2022-11-19
     * @Time:  18:00
     * @Param: [request]
     * @return: com.lkx.common.R<java.lang.String>
     * @desc: 员工退出
     **/
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功！");
    }
    //新增员工
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工，员工信息：{}",employee);
        //employee.setCreateTime(LocalDateTime.now());
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getIdNumber().substring(12).getBytes(StandardCharsets.UTF_8)));
        //employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute("employee");
        // employee.setCreateUser(empId);
        //employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增员工成功！");
    }
    //分页查询
   // @Cacheable(value = "cacheSpace",key ="#page" )
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("page={},pageNum={},pageSize={}",page,pageSize,name);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();

        lqw.notIn(Employee::getUsername,"admin");//排除超级管理员
        lqw.like(!StringUtils.isEmpty(name),Employee::getName,name);
        //添加排序条件
        lqw.orderByDesc(Employee::getCreateTime);
        employeeService.page(pageInfo,lqw);

        return R.success(pageInfo);

    }
    /**
     * @Date:  2022-11-20
     * @Time:  19:47
     * @Param: []
     * @return: com.lkx.common.R<java.lang.String>
     * @desc: 根据id修改员工信息
     **/

    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        long id = Thread.currentThread().getId();
        log.info("id为{}",id);
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(empId);
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("员工信息修改成功！");
    }

    @GetMapping("/{id}")
    //@Cacheable(value = "pool",key = "#id")
    public R<Employee> getById(@PathVariable("id") Long id){
        log.info("根据id查询员工信息");
        Employee employee = employeeService.getById(id);
       if (employee!=null){
           return R.success(employee);
       }
       return R.error("没有查询到此员工信息");
    }
}
